import { Component,Injector,Inject} from '@angular/core';
import {OverlayContainer} from "@angular/cdk/overlay";
import {environment} from "../environments/environment"
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  darkTheme = false;
  constructor(private oc:OverlayContainer,@Inject('BASE_CONFIG') config){
     const injector = Injector.create(
       // Person 简写的形式
       {
         providers: [
           {provide: Person, deps: []},
           {provide: Address,useFactory:()=>{
             if(environment.production){
               return new Address('北京','北京','朝阳区','xx街道');
             }else{
               return new Address('西藏','拉萨','xx','xxx');
             }
           },deps: []},
           {provide:Id,useFactory:()=>{
             return Id.getInstance('i dcard')
           },deps: []},
         ]
       }
     );
     const person = injector.get(Person);
     console.log(config);
     console.log(person);
  }
  switchTheme(dark){
    this.darkTheme = dark;
    this.oc.getContainerElement().classList.toggle('myapp-dark-theme');
    // this.oc.getContainerElement().className = dark? 'cdk-overlay-container myapp-dark-theme':'cdk-overlay-container';
  }
}
class Id{
  static getInstance(type:string):Id{
    //设置
    return new Id();
  }
}
class Address{
  province:string ;
  city:string ;
  district:string ;
  street:string ;
  constructor(province,city,district,street){
    this.province = province;
    this.city = city;
    this.district = district;
    this.street = street;
  }
}
class Person{
  id:Id;
  address:Address
  /*
  constructor(){
    this.id = Id.getInstance('idcard');
    this.address = new Address('北京','北京','朝阳区','xx街道');
  }
  */
  /*
  constructor(id:Id,address:Address){
    this.id = id ;
    this.address = address;
  }
  */
  constructor(@Inject(Id) id,@Inject(Address) address){
    this.id = id;
    this.address = address;
  }
}
function main(){
  const id  = Id.getInstance('idcard');
  const address = new Address('北京','北京','朝阳区','xx街道')
  const person = new Person(id,address);
}
